<template>
  <div class>
    <div class="card tr mb2">
      <el-popover placement="bottom" width="200" trigger="click">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <el-checkbox-group v-model="showItemList" @change="handleCheckedChange">
          <el-checkbox v-for="(item,key) in allItemList" :key="key" :label="item">{{item.name}}</el-checkbox>
        </el-checkbox-group>
        <el-button slot="reference" icon="el-icon-menu" size="mini" type="primary" circle></el-button>
      </el-popover>
      <el-button icon="el-icon-search" @click="formShow= !formShow" size="mini" type="primary" circle></el-button>
      <el-button icon="el-icon-download" @click="handleDownload" size="mini" type="primary" circle></el-button>
    </div>
    <div class="card" v-show="formShow">
      <el-form size="mini" :style="style" :model="ruleForm" ref="ruleForm" inline label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="表名" prop="tableName">
          <template>
            <el-select v-model="ruleForm.tableName" clearable filterable style="width:200px" placeholder="请选择">
              <el-option
                v-for="item in tableNameList"
                :key="item.tableName"
                :label="item.tableName"
                :value="item.tableName">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="创建人" prop="tableName">
          <template>
            <el-select v-model="ruleForm.createUserId" clearable filterable style="width:200px" placeholder="请选择">
              <el-option
                v-for="item in users"
                :key="item.id"
                :label="item.realName"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">查询</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="60px" align="center" fixed="left" label="序号"></el-table-column>
      <el-table-column v-for="(item,index) in showItemList" :width="item.width || '' " :key="index" :label="item.name">
        <template slot-scope="scope">
          <template v-if="!item.formatter">{{ scope.row[item.key] }}</template>
          <template v-else>
            <span v-text="item.formatter(scope.row, item)"></span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showDDL(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next"
        :total="total"></el-pagination>
    </el-card>
    <el-dialog title="创建语句" :close-on-click-modal="false" :visible.sync="dialogVisible">
          <el-input size="mini" type="textarea" v-model="searchDDL" :rows="18"></el-input>
          <div slot="footer" class="dialog-footer">
            <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          </div>
    </el-dialog>
  </div>
</template>

<script>
  const defaultFormThead = [
    { key: "tableName", name: "表名" },
    { key: "createUserName", name: "创建人" },
    {
      key: "createTime", name: "创建时间",
      formatter: row => {
        if (row.createTime) {
          return (new Date(row.createTime)).toLocaleString();
        } else {
          return '';
        }
      }
    },
  ];
  import EnumSelector from '@/pages/comm/enumSelector'
  export default {
    components: { EnumSelector },
    data() {
      return {
        dialogVisible:false,
        searchDDL:'',
        users:[],
        tableNameList:[],
        checkAll: true,
        isIndeterminate: false,
        allItemList: defaultFormThead,
        showItemList: defaultFormThead,
        formShow: false,
        offForm: true, //是否折叠
        folding: false, //表单是否超过一行
        tableData: [],
        total: 0,
        pageSize: 10,
        currentPage: 1,
        ruleForm: {
          tableName: '',
          createUserId:'',
        },
        style: {},
      };
    },

    mounted() {
      this.init();
      this.getTableNameList();
      this.getusers();
      let formHeight = this.$refs.ruleForm.$el.clientHeight;
      if (formHeight > 62) {
        this.folding = true;
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.folding = false;
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    },
    computed: {
    },
    watch: {
      offForm: function (v) {
        if (v) {
          this.style = {
            height: "62px",
            overflow: "hidden"
          };
        } else {
          this.style = {
            height: "auto",
            overflow: "hidden"
          };
        }
      }
    },
    methods: {
      showDDL(index,row){
        this.dialogVisible=true;
        let params={
          tableName:row.tableName,
        }
        this.$api
              .formPost("/api/code/codeTableInfo/doGetCreateDDL", params)
              .then(res => {
                  if (res.retHead.code == "0000000") {
                    // console.log(res.retBody);
                      this.searchDDL=res.retBody.ddlSql;
                  }
              });
      },
      getusers() {
        let params = {};
        this.$api.formPost("/api/auth/sysuser/doGetList", params).then(res => {
          if (res.retHead.code == "0000000") {
            this.users = res.retBody.list;
          }
        });
      },
      getTableNameList(){
      this.$api
              .formPost("/api/code/codeTableInfo/getTableNameList", {})
              .then(res => {
                  if (res.retHead.code == "0000000") {
                    // console.log(res.retBody);
                      this.tableNameList=res.retBody;
                  }
              });
      },
      handleDownload() {
        this.downloadLoading = true;
        import("@/vendor/Export2Excel").then(excel => {
          const tHeader = [
            "主键",
            "表名",
            "创建人ID",
            "创建人",
            "创建时间",
          ];
          const filterVal = [
            "id",
            "tableName",
            "createUserId",
            "createUserName",
            "createTime",
          ];
          const data = this.formatJson(filterVal, this.tableData);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: "table-list"
          });
          this.downloadLoading = false;
        });
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            if (j === "timestamp") {
              return parseTime(v[j]);
            } else {
              return v[j];
            }
          })
        );
      },
      handleCheckedChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === defaultFormThead.length;
        this.isIndeterminate =
          checkedCount > 0 && checkedCount < defaultFormThead.length;
      },
      handleCheckAllChange(val) {
        this.showItemList = val ? defaultFormThead : [];
        this.isIndeterminate = false;
      },
      sizeChange(n) {
        this.pageSize = n;
        this.init();
      },
      currentChange(n) {
        this.currentPage = n;
        this.init();
      },
      init() {
        let searchForm = {};
        searchForm.tableName = this.ruleForm.tableName;
        searchForm.createUserId=this.ruleForm.createUserId;
        let params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...searchForm
        };
        this.$api
          .formPost("/api/code/createTableRecord/doGetList", params)
          .then(res => {
            if (res.retHead.code == "0000000") {
              this.tableData = res.retBody.list;
              console.log(this.tableData);
              this.total = res.retBody.total;
              this.currentPage = res.retBody.pageNum;
            }
          });
      },

      submitForm(formName) {
        this.init();
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() { }
  };
</script>
<style lang='scss' scoped>
  .btns {
    text-align: right;
  }

  .icon-arrow {
    position: absolute;
    bottom: 5px;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #f1f1f1;

    i {
      background-color: #fff;
      position: absolute;
      color: #999;
      bottom: -7px;
      margin-left: 50%;
      transform: translateX(-50%);
      cursor: pointer;
    }
  }
</style>